package com.zimmur.service.impl.zone;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import javax.annotation.Resource;  
  
import org.springframework.stereotype.Service;

import com.zimmur.common.EnumPointRule;
import com.zimmur.dao.zone.ITopicCollectDao;
import com.zimmur.dao.zone.ITopicDao;
import com.zimmur.model.account.GoodsUser;
import com.zimmur.model.zone.Topic;
import com.zimmur.model.zone.TopicCollect;
import com.zimmur.service.IUserService;
import com.zimmur.service.account.IPointLogService;
import com.zimmur.service.zone.ITopicCollectService;
  
  
@Service("topicCollectService")  
public class TopicCollectServiceImpl implements ITopicCollectService {  
    @Resource  
    private ITopicCollectDao topicCollectDao;
    @Resource  
    private ITopicDao topicDao;
    @Resource
    private IUserService userService;
	@Resource
    private IPointLogService pointService;

	@Override
	public int insertTopicCollect(TopicCollect topicCollec) {
		return topicCollectDao.insertTopicCollect(topicCollec);
	}
	@Override
	public int deleteTopicCollect(TopicCollect topicCollec) {
		return topicCollectDao.deleteTopicCollect(topicCollec);
	}
	@Override
	public int updateTopicCollect(TopicCollect topicCollec) {
		return topicCollectDao.updateTopicCollect(topicCollec);
	}

	@Override
	public TopicCollect selectTopicCollect(TopicCollect topicCollec) {
		return topicCollectDao.selectTopicCollect(topicCollec);
		
	}
	@Override
	public int collectWhether(TopicCollect topicCollects) {
		Integer resut=0;
		try {
			TopicCollect topicCollec = topicCollectDao.selectTopicCollect(topicCollects);
		  
	    	long topicId = topicCollects.getTopicId();
	    	Topic topic = topicDao.selectByIdstopic(topicId);
	        if(null == topicCollec){
			    resut= topicCollectDao.insertTopicCollect(topicCollects);
			    topic.setFavNum(topic.getFavNum()+1);
	        }
	        if(null !=  topicCollec && topicCollec.getStatus().equals(1)){
	        	topicCollects.setStatus(0);
	        	resut= topicCollectDao.updateTopicCollect(topicCollects);
	        	Integer favNum=topic.getFavNum()-1;
	        	topic.setFavNum(favNum);
	        }
	        if(null !=  topicCollec && topicCollec.getStatus().equals(0)){
	        	topicCollects.setStatus(1);
	        	resut= topicCollectDao.updateTopicCollect(topicCollects);
	        	Integer favNum=topic.getFavNum()+1;
	        	topic.setFavNum(favNum);
	        }
	        topicDao.updateStopicFavNum(topic);
	        
	        topicFavPoint(topicCollec.getUserId());
		} catch (Exception e) {
			e.getMessage();
		}
		return resut;
	}
    /**
     * 用户话题被收藏,积分增加规则
     * @param userId
     */
	private void topicFavPoint(long userId){
		if(userId>0){
			GoodsUser fromUser = userService.selectByUserId(userId);
			if(fromUser.getCategoryCode().equals("zk")){
				pointService.mainPointLog(userId, "话题收藏", EnumPointRule.ZK_FAV, 0);
			}
		}
	}
}  
